package models

import (
	"bigdevops/src/common"
	"bigdevops/src/config"
	"encoding/json"
	"fmt"
)

var (
	mockScriptContentNoArgs = `
kubectl get node 
`

	mockScriptContentWithArgs = `
kubectl get node $1
`

	mockScriptContentSleep = `
date 
echo hello
sleep 100
`

	mockScriptContentNoRoot = `
pwd
ls -l
echo nyy
date > /root/abc

`
	mockScriptContentPythonNoArgs = `#!/usr/bin/env python3
import time

print("哈哈.sleep1000")
time.sleep(1000)
`

	mockScriptContentPythonWithArgs = `#!/usr/bin/env python3
import sys


if __name__ == "__main__":
    args = sys.argv
    print("参数", args)
    print("加法结果", int(args[1]) + 10)

`

	mockCodePython = `
#!/usr/bin/env python3
import os
import sys      
try:
    directory = sys.argv[1]   
except IndexError:
    sys.exit("Must provide an argument.")

dir_size = 0   
fsizedicr = {'Bytes': 1,
             'Kilobytes': float(1) / 1024,
             'Megabytes': float(1) / (1024 * 1024),
             'Gigabytes': float(1) / (1024 * 1024 * 1024)}
for (path, dirs, files) in os.walk(directory):      
    for file in files:                              
        filename = os.path.join(path, file)
        dir_size += os.path.getsize(filename)       

fsizeList = [str(round(fsizedicr[key] * dir_size, 2)) + " " + key for key in fsizedicr] 

if dir_size == 0: print ("File Empty") 
else:
  for units in sorted(fsizeList)[::-1]: 
      print ("Folder Size: " + units)
`
	mockScriptContents = []string{
		mockScriptContentNoArgs,
		mockScriptContentWithArgs,
		mockScriptContentSleep,
		mockScriptContentPythonNoArgs,
		mockScriptContentPythonWithArgs,
	}
)

func MockJobExec(sc *config.ServerConfig) {

	// 造脚本
	num := 5

	randLang := []string{"python", "shell", "json", "yaml", "debian"}

	for i := 1; i < num; i++ {

		tmp := JobScript{
			Name:   fmt.Sprintf("脚本-%v", i),
			Lang:   randLang[common.Frn(len(randLang)-1)],
			UserID: 1,
			Code:   mockScriptContents[common.Frn(len(mockScriptContents)-1)],
		}
		tmp.CreateOne()

	}

	hosts := []string{
		"192.168.0.81",
	}
	for i := 1; i < num; i++ {
		hosts = append(hosts,

			fmt.Sprintf("192.168.0.8%d", 1+i),
		)
	}

	hostsJson, _ := json.Marshal(hosts)
	for i, c := range mockScriptContents {

		job := JobTask{
			Title:              fmt.Sprintf("测试的job-%v", i),
			Account:            "root",
			Args:               "",
			ScriptContent:      c,
			ExecTimeoutSeconds: 120,
			HostsRaw:           string(hostsJson),
			BatchSize:          i,
			TotalNum:           len(hosts),
			Action:             "",
			OnErrorStrategy:    common.JOB_ONERROR_STRATEGY_PAUSE,
			//Status:             common.JOB_STATUS_PENING,
			Status: common.JOB_STATUS_PENING,
			UserID: 1,
		}
		job.CreateOne()
	}

}
